package com.zjdiepu.www.biz.icbc;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.diepu.www.client.proxy.partner.request.own.DiepuYwOpenCardHzicbcRequest;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.OpenCardStatus;
import com.zjdiepu.www.base.enums.type.AttachmentLabel;
import com.zjdiepu.www.base.enums.type.AttachmentSort;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.dao.dh.DhLoanAfterMapper;
import com.zjdiepu.www.dao.dz.DzLoanMiddleBankMapper;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcLogMapper;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcMapper;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcSubMapper;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcSubmitInfoMapper;
import com.zjdiepu.www.dao.yw.YwBusinessBuyerMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcLogService;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcService;
import com.zjdiepu.www.model.base.dto.OtherAttachments;
import com.zjdiepu.www.model.base.ext.OtherAttachmentsExt;
import com.zjdiepu.www.model.dh.dto.DhLoanAfter;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleBank;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbc;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcLog;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcSub;
import com.zjdiepu.www.model.icbc.ext.YwOpenCardHzicbcExt;
import com.zjdiepu.www.model.icbc.vo.YwOpenCardHzicbcSubVo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.proxy.DiepuOpenCardHzicbcService;
import com.zjdiepu.www.response.BooleanResponse;
import com.zjdiepu.www.security.exception.BasePartnerException;
import com.zjdiepu.www.security.server.core.Partners;

/**
 * yw_开卡信息_杭州工行Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年12月09日 11时48分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwOpenCardHzicbcServiceImpl extends BaseService implements IYwOpenCardHzicbcService {
	public static final Logger logger = LoggerFactory.getLogger(YwOpenCardHzicbcServiceImpl.class);

	@Autowired
	private YwOpenCardHzicbcMapper ywOpenCardHzicbcMapper;
	@Autowired
	private YwOpenCardHzicbcSubMapper ywOpenCardHzicbcMapperSub;
	@Autowired
	private DzLoanMiddleBankMapper dzLoanMiddleBankMapper;
	@Autowired
	private DhLoanAfterMapper dhLoanAfterMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;
	
	@Resource
	private DiepuOpenCardHzicbcService diepuOpenCardHzicbcService;
	@Autowired
	private YwBusinessBuyerMapper ywBusinessBuyerMapper;
	
	@Resource
	private IOtherAttachmentsService otherAttachmentsService;
	@Autowired
	private YwOpenCardHzicbcSubmitInfoMapper ywOpenCardHzicbcSubmitInfoMapper;
	
	@Resource
	private IYwOpenCardHzicbcLogService ywOpenCardHzicbcLogService;
	
	@Autowired
	private YwOpenCardHzicbcLogMapper ywOpenCardHzicbcLogMapper;
	
	@Override
	public void saveUpdateAndDelete(YwOpenCardHzicbc ywOpenCardHzicbc, OperateType operType) {
		switch (operType) {
		case add:
			ywOpenCardHzicbc.initInsertData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
			ywOpenCardHzicbcMapper.insert(ywOpenCardHzicbc);
			break;
		case edit:
			ywOpenCardHzicbc.initUpdateData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
			// 影响的记录数
			int updateCount = ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywOpenCardHzicbc.initDeleteData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
			// 影响的记录数
			int deleteCount = ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwOpenCardHzicbc> selectByExt(YwOpenCardHzicbcExt ywOpenCardHzicbcExt) {
		return ywOpenCardHzicbcMapper.selectByExt(ywOpenCardHzicbcExt);
	}
	
	@Override
	public int countByExt(YwOpenCardHzicbcExt ywOpenCardHzicbcExt){
		return ywOpenCardHzicbcMapper.countByExt(ywOpenCardHzicbcExt);
	}
	
	@Override
	public YwOpenCardHzicbc selectById(String id) {
		return ywOpenCardHzicbcMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywOpenCardHzicbcMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwOpenCardHzicbc ywOpenCardHzicbc) {
		return ywOpenCardHzicbcMapper.countRefined(ywOpenCardHzicbc);
	}

	@Override
	public YwOpenCardHzicbc selectRefined(YwOpenCardHzicbc ywOpenCardHzicbc) {
		return ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc);
	}

	@Override
	public boolean repeat(YwOpenCardHzicbc ywOpenCardHzicbc) {
		int count = countRefined(ywOpenCardHzicbc);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywOpenCardHzicbc.getId())) {
			return true;
		}
		YwOpenCardHzicbc dbItem = selectRefined(ywOpenCardHzicbc);
		if(null == dbItem) {
			return false;
		}
		return !ywOpenCardHzicbc.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywOpenCardHzicbcMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public boolean updateOpenCardBackReason(YwOpenCardHzicbc ywOpenCardHzicbc) {
		
		ywOpenCardHzicbc.setModifier(SystemConsts.DEFAULT_ID);
//		ywOpenCardHzicbc.setHzicbcOpenCardStatus(OpenCardStatus.KAI_KA_SHI_BAI.getCode().toString());
		ywOpenCardHzicbc.setOpenCardStatus(ywOpenCardHzicbc.getOpenCardStatus());
		ywOpenCardHzicbc.setModifierName("开卡退回");
		ywOpenCardHzicbc.setModified((int)(System.currentTimeMillis() / 1000));
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness !=null){
			ywBusiness.setOpenCardStatus(ywOpenCardHzicbc.getOpenCardStatus());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public void saveOpenCardSuccess(YwOpenCardHzicbc ywOpenCardHzicbc) {
		//更新【yw_open_card_hzicbc】的开卡日期、卡号、对账单日、还款日、开卡状态（开卡成功）；
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbc.getBusiId());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1!=null){
			ywOpenCardHzicbc1.setOpenDate(ywOpenCardHzicbc.getOpenDate());
			ywOpenCardHzicbc1.setCardno(ywOpenCardHzicbc.getCardno());
			ywOpenCardHzicbc1.setBankStatementDate(ywOpenCardHzicbc.getBankStatementDate());
			ywOpenCardHzicbc1.setRepaymentDate(ywOpenCardHzicbc.getRepaymentDate());
			ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.KAI_KA_CHENG_GONG.getCode());
			ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
		}
		
		//更新（没有就新增）【dz_loan_middle_bank】的 还款卡号；
		DzLoanMiddleBank dzLoanMiddleBank=new DzLoanMiddleBank();
		dzLoanMiddleBank.setBusiId(ywOpenCardHzicbc.getBusiId());
		dzLoanMiddleBank=dzLoanMiddleBankMapper.selectRefined(dzLoanMiddleBank);
		if(dzLoanMiddleBank!=null){
			dzLoanMiddleBank.setCardnoRefund(ywOpenCardHzicbc.getCardno());
			dzLoanMiddleBankMapper.updateByPrimaryKeySelective(dzLoanMiddleBank);
		}else{
			DzLoanMiddleBank dzLoanMiddleBank1=new DzLoanMiddleBank();
			dzLoanMiddleBank1.setBusiId(ywOpenCardHzicbc.getBusiId());
			dzLoanMiddleBank1.setCardnoRefund(ywOpenCardHzicbc.getCardno());
			dzLoanMiddleBank1.initInsertData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
			dzLoanMiddleBankMapper.insert(dzLoanMiddleBank1);
		}
		YwBusiness ywBusiness=new YwBusiness();
		ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness !=null){
			ywBusiness.setOpenCardStatus(OpenCardStatus.KAI_KA_CHENG_GONG.getCode());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
		
		//更新（没有就新增）【dh_loan_after】的 还款日；
		DhLoanAfter dhLoanAfter=new DhLoanAfter();
		dhLoanAfter.setBusiId(ywOpenCardHzicbc.getBusiId());
		dhLoanAfter=dhLoanAfterMapper.selectRefined(dhLoanAfter);
		if(dhLoanAfter!=null){
			dhLoanAfter.setDateRepayment(ywOpenCardHzicbc.getRepaymentDate());
			dhLoanAfterMapper.updateByPrimaryKeySelective(dhLoanAfter);
		}else{
			DhLoanAfter dhLoanAfter1=new DhLoanAfter();
			dhLoanAfter1.setBusiId(ywOpenCardHzicbc.getBusiId());
			dhLoanAfter1.setDateRepayment(ywOpenCardHzicbc.getRepaymentDate());
			dhLoanAfter1.initInsertData(SystemConsts.DEFAULT_ID, "开卡成功通知");
			dhLoanAfterMapper.insert(dhLoanAfter1);
		}
	}
	@Override
	public void commitDaishenheYwOpenCardHzicbc(YwOpenCardHzicbc ywOpenCardHzicbc,AbstractSysUser user) {
		//将 yw_business 的 open_card_status 修改为‘50’、
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		String bbusiId=ywBusiness.getId();
		String cusId=ywBusiness.getCustomerId();
		ywBusiness.setOpenCardStatus(OpenCardStatus.DAI_SHEN_HE.getCode());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		
		//将 yw_open_card_hzicbc 的 open_card_status 修改为‘50’、
		ywOpenCardHzicbc.initUpdateData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
		ywOpenCardHzicbc.setOpencardSubmitTime(new Date());
		ywOpenCardHzicbc.setOpenCardStatus(OpenCardStatus.DAI_SHEN_HE.getCode());
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
		
		
		YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
		ywOpenCardHzicbcLog.initInsertData(user.getId(), user.getName());
		ywOpenCardHzicbcLog.setBusiId(bbusiId);
		ywOpenCardHzicbcLog.setCusId(cusId);
		ywOpenCardHzicbcLog.setLogType(1);
		ywOpenCardHzicbcLog.setRemarks(ywOpenCardHzicbc.getReasonBack());
		ywOpenCardHzicbcLog.setOpencardStatus(OpenCardStatus.DAI_SHEN_HE.getCode().toString());
		ywOpenCardHzicbcLogMapper.insert(ywOpenCardHzicbcLog);
		
		//暂时取消
//		YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo=new YwOpenCardHzicbcSubmitInfo();
//		ywOpenCardHzicbcSubmitInfo.setDpStatus(DpStatus.NORMAL.getCode());
//		ywOpenCardHzicbcSubmitInfo.setBusiId(bbusiId);
//		ywOpenCardHzicbcSubmitInfo=ywOpenCardHzicbcSubmitInfoMapper.selectRefined(ywOpenCardHzicbcSubmitInfo);
//		if(ywOpenCardHzicbcSubmitInfo!=null){
//			ywOpenCardHzicbcSubmitInfo.initUpdateData(user.getId(), user.getName());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitTime(new Date());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperatorId(user.getId());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperator(user.getName());
//			ywOpenCardHzicbcSubmitInfoMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSubmitInfo);
//		}else{
//			ywOpenCardHzicbcSubmitInfo=new YwOpenCardHzicbcSubmitInfo();
//			ywOpenCardHzicbcSubmitInfo.setBusiId(bbusiId);
//			ywOpenCardHzicbcSubmitInfo.setCusId(cusId);
//			ywOpenCardHzicbcSubmitInfo.initInsertData(user.getId(), user.getName());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitTime(new Date());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperatorId(user.getId());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperator(user.getName());
//			ywOpenCardHzicbcSubmitInfoMapper.insert(ywOpenCardHzicbcSubmitInfo);
//		}
	}
	

	@Override
	public BooleanResponse commitYwOpenCardHzicbc(YwOpenCardHzicbcSub ywOpenCardHzicbcVo,
			AbstractSysUser user) {
		DiepuYwOpenCardHzicbcRequest diepuYwOpenCardHzicbcRequest=new DiepuYwOpenCardHzicbcRequest();
		//将 yw_open_card_hzicbc 的 open_card_status 修改为‘100’、
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		BeanUtils.copyProperties(ywOpenCardHzicbc1, diepuYwOpenCardHzicbcRequest);
		ywOpenCardHzicbc1.initUpdateData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
		ywOpenCardHzicbc1.setOpencardSubmitTime(new Date());
		ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.YI_TI_JIAO.getCode());
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
		
//		String bbusiId=ywOpenCardHzicbc1.getBusiId();
//		String cusId=ywOpenCardHzicbc1.getCusId();
//		YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo=new YwOpenCardHzicbcSubmitInfo();
//		ywOpenCardHzicbcSubmitInfo.setDpStatus(DpStatus.NORMAL.getCode());
//		ywOpenCardHzicbcSubmitInfo.setBusiId(bbusiId);
//		ywOpenCardHzicbcSubmitInfo=ywOpenCardHzicbcSubmitInfoMapper.selectRefined(ywOpenCardHzicbcSubmitInfo);
//		if(ywOpenCardHzicbcSubmitInfo!=null){
//			ywOpenCardHzicbcSubmitInfo.initUpdateData(user.getId(), user.getName());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitTime(new Date());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperatorId(user.getId());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperator(user.getName());
//			ywOpenCardHzicbcSubmitInfoMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSubmitInfo);
//		}else{
//			ywOpenCardHzicbcSubmitInfo=new YwOpenCardHzicbcSubmitInfo();
//			ywOpenCardHzicbcSubmitInfo.setBusiId(bbusiId);
//			ywOpenCardHzicbcSubmitInfo.setCusId(cusId);
//			ywOpenCardHzicbcSubmitInfo.initInsertData(user.getId(), user.getName());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitTime(new Date());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperatorId(user.getId());
//			ywOpenCardHzicbcSubmitInfo.setAttSubmitOperator(user.getName());
//			ywOpenCardHzicbcSubmitInfoMapper.insert(ywOpenCardHzicbcSubmitInfo);
//		}
		
		YwOpenCardHzicbcSub ywOpenCardHzicbcSub1=new YwOpenCardHzicbcSub();
		ywOpenCardHzicbcSub1.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywOpenCardHzicbcSub1=ywOpenCardHzicbcMapperSub.selectRefined(ywOpenCardHzicbcSub1);
		if(ywOpenCardHzicbcSub1!=null){
			BeanUtils.copyProperties(ywOpenCardHzicbcSub1, diepuYwOpenCardHzicbcRequest);
			ywOpenCardHzicbcSub1.initUpdateData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbcMapperSub.updateByPrimaryKeySelective(ywOpenCardHzicbcSub1);
		}
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbcVo.getBusiId());
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywBusiness.setOpenCardStatus(OpenCardStatus.YI_TI_JIAO.getCode());
		ywBusiness.setTimeApplyOpencard(new Date());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		YwBusinessLoan ywBusinessLoan=new YwBusinessLoan();
		ywBusinessLoan.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywBusinessLoan=ywBusinessLoanMapper.selectRefined(ywBusinessLoan);
		
		OtherAttachmentsExt otherAttachmentsExt=new OtherAttachmentsExt();
		otherAttachmentsExt.setDpStatus(DpStatus.NORMAL.getCode());
		otherAttachmentsExt.setRows(1);
		otherAttachmentsExt.setDataid(ywBusiness.getId());
		otherAttachmentsExt.setIncludeFileLabels(Arrays.asList(AttachmentLabel.HZ_KAIKA_SQBIAO.getCode(),AttachmentLabel.EDU_HEDING_SQBIAO.getCode()));
		List<OtherAttachments> list=otherAttachmentsService.selectByExt(otherAttachmentsExt);
		if(list==null || list.size()==0){
			return new BooleanResponse(false);
		}
		
		String wordFileName = "开卡申请表_"+ywBusiness.getCustomerName()+"_"+ywBusiness.getCustomerNo()+".doc";
		otherAttachmentsService.uploadWord(user, wordFileName, ModeIDType.SQKK, AttachmentSort.HZ_YIN_HANG_KAIKA_ZILIAO, 
				AttachmentLabel.KAI_KA_SHEN_QING_BIAO_WORD,ywBusiness.getId(), ywBusiness.getQueryCreditApplyId(), null, 
				Arrays.asList(AttachmentLabel.SHEN_FEN_ZHENG_MING_FAN_MIAN.getCode(),AttachmentLabel.SHEN_FEN_ZHENG_MING_ZHENG_MIAN.getCode(),
						AttachmentLabel.HZ_KAIKA_SQBIAO.getCode()), null);
		//保存购车人信息
		YwBusinessBuyer ywBusinessBuyer=new YwBusinessBuyer();
		ywBusinessBuyer.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywBusinessBuyer.setCarManType(CreditPeopleType.ZHU_JIE_REN.getCode());
		ywBusinessBuyer=ywBusinessBuyerMapper.selectRefined(ywBusinessBuyer);
		//调用中转开卡接口，如果成功就返回
		
		try {
			if(ywBusinessLoan!=null){
				BeanUtils.copyProperties(ywBusinessLoan, diepuYwOpenCardHzicbcRequest);
			}
			if(ywBusinessBuyer!=null){
				BeanUtils.copyProperties(ywBusinessBuyer, diepuYwOpenCardHzicbcRequest);
			}
			diepuYwOpenCardHzicbcRequest.setCcol1(ywOpenCardHzicbcSub1.getCcol1());
//			BeanUtils.copyProperties(ywOpenCardHzicbcVo, diepuYwOpenCardHzicbcRequest);
			BooleanResponse booleanResponse=diepuOpenCardHzicbcService.applyIcbcOpenCardInterface(diepuYwOpenCardHzicbcRequest,Partners.HZ_ICBC);
			if(booleanResponse.getResult()==true){
				YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
				ywOpenCardHzicbcLog.initInsertData(user.getId(), user.getName());
				ywOpenCardHzicbcLog.setBusiId(ywBusiness.getId());
				ywOpenCardHzicbcLog.setCusId(ywBusiness.getCustomerId());
				ywOpenCardHzicbcLog.setLogType(1);
				ywOpenCardHzicbcLog.setOpencardStatus(OpenCardStatus.KAI_KA_CHENG_GONG.getCode().toString());
				ywOpenCardHzicbcLogMapper.insert(ywOpenCardHzicbcLog);
			}
			return booleanResponse;
		} catch (BasePartnerException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void saveYwOpenCardHzicbc(YwOpenCardHzicbcSubVo ywOpenCardHzicbcVo,AbstractSysUser currentUser) {
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1!=null){
			ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.YI_BAO_CUN.getCode());
			ywOpenCardHzicbc1.initUpdateData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
		}else{
			ywOpenCardHzicbc1=new YwOpenCardHzicbc();
			ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbcVo.getBusiId());
			ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.YI_BAO_CUN.getCode());
			ywOpenCardHzicbc1.initInsertData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbc1.setCusId(ywOpenCardHzicbcVo.getCusId());
			ywOpenCardHzicbcMapper.insert(ywOpenCardHzicbc1);
			
		}
		YwOpenCardHzicbcSub ywOpenCardHzicbcSub1=new YwOpenCardHzicbcSub();
		ywOpenCardHzicbcSub1.setBusiId(ywOpenCardHzicbcVo.getBusiId());
		ywOpenCardHzicbcSub1=ywOpenCardHzicbcMapperSub.selectRefined(ywOpenCardHzicbcSub1);
		if(ywOpenCardHzicbcSub1!=null){
			YwOpenCardHzicbcSub ywOpenCardHzicbcSub=new YwOpenCardHzicbcSub();
			BeanUtils.copyProperties(ywOpenCardHzicbcVo, ywOpenCardHzicbcSub);
			ywOpenCardHzicbcSub.setId(ywOpenCardHzicbcSub1.getId());
			ywOpenCardHzicbcSub.initUpdateData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbcSub.setOpenCardId(ywOpenCardHzicbc1.getId());
			ywOpenCardHzicbcMapperSub.updateByPrimaryKeySelective(ywOpenCardHzicbcSub1);
		}else{
			YwOpenCardHzicbcSub ywOpenCardHzicbcSub=new YwOpenCardHzicbcSub();
			BeanUtils.copyProperties(ywOpenCardHzicbcVo, ywOpenCardHzicbcSub);
			ywOpenCardHzicbcSub.initInsertData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbcSub.setOpenCardId(ywOpenCardHzicbc1.getId());
			ywOpenCardHzicbcSub.setCusId(ywOpenCardHzicbcVo.getCusId());
			ywOpenCardHzicbcMapperSub.insert(ywOpenCardHzicbcSub);
		}
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbcVo.getBusiId());
		if(ywBusiness!=null){
			ywBusiness.setOpenCardStatus(OpenCardStatus.YI_BAO_CUN.getCode());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
	}

	@Override
	public void updateOpenCardInfo(YwOpenCardHzicbcSubVo ywOpenCardHzicbcVo) {
		String busiId=ywOpenCardHzicbcVo.getBusiId();
		YwOpenCardHzicbcSub ywOpenCardHzicbcSub2=new YwOpenCardHzicbcSub();
		ywOpenCardHzicbcSub2.setBusiId(busiId);
		ywOpenCardHzicbcSub2.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbcSub2=ywOpenCardHzicbcMapperSub.selectRefined(ywOpenCardHzicbcSub2);
		if(ywOpenCardHzicbcSub2!=null){
			YwOpenCardHzicbcSub ywOpenCardHzicbcSub=new YwOpenCardHzicbcSub();
			BeanUtils.copyProperties(ywOpenCardHzicbcVo, ywOpenCardHzicbcSub);
			ywOpenCardHzicbcSub.initUpdateData(ywOpenCardHzicbcVo.getModifier(), ywOpenCardHzicbcVo.getModifierName());
			ywOpenCardHzicbcSub.setId(ywOpenCardHzicbcSub2.getId());
			ywOpenCardHzicbcMapperSub.updateByPrimaryKeySelective(ywOpenCardHzicbcSub);
		}
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbcVo.getBusiId());
		if(ywBusiness!=null){
			ywBusiness.setOpenCardStatus(OpenCardStatus.YI_BAO_CUN.getCode());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
			
			YwOpenCardHzicbc ywOpenCardHzicbc=new YwOpenCardHzicbc();
			ywOpenCardHzicbc.setBusiId(busiId);
			ywOpenCardHzicbc=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc);
			if(ywOpenCardHzicbc==null){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			}
			ywOpenCardHzicbc.initUpdateData(ywOpenCardHzicbc.getModifier(), ywOpenCardHzicbc.getModifierName());
			ywOpenCardHzicbc.setOpencardSubmitTime(new Date());
			ywOpenCardHzicbc.setOpenCardStatus(OpenCardStatus.YI_BAO_CUN.getCode());
			ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
		}
		
	}

	@Override
	public void updateBackOpenCardStatus(YwOpenCardHzicbc ywOpenCardHzicbc,AbstractSysUser user) {
		String reasonBack=ywOpenCardHzicbc.getReasonBack();
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbc.getBusiId());
		ywOpenCardHzicbc1.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbc=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywOpenCardHzicbc.setOpenCardStatus(OpenCardStatus.TUI_HUI.getCode());
		ywOpenCardHzicbc.setReasonBack(reasonBack);
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywBusiness.setOpenCardStatus(OpenCardStatus.TUI_HUI.getCode());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		
		YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
		ywOpenCardHzicbcLog.initInsertData(user.getId(), user.getName());
		ywOpenCardHzicbcLog.setBusiId(ywBusiness.getId());
		ywOpenCardHzicbcLog.setCusId(ywBusiness.getCustomerId());
		ywOpenCardHzicbcLog.setLogType(1);
		ywOpenCardHzicbcLog.setOpencardStatus(OpenCardStatus.TUI_HUI.getCode().toString());
		ywOpenCardHzicbcLog.setRemarks(reasonBack);
		ywOpenCardHzicbcLogService.saveUpdateAndDelete(ywOpenCardHzicbcLog, OperateType.add);
	}

	@Override
	public void updateOpencardLine(YwOpenCardHzicbc ywOpenCardHzicbc,
			AbstractSysUser currentUser) {
		String busiId=ywOpenCardHzicbc.getBusiId();;
		String cusId=ywOpenCardHzicbc.getCusId();
		DzLoanMiddleBank dzLoanMiddleBank=new DzLoanMiddleBank();
		dzLoanMiddleBank.setBusiId(busiId);
		dzLoanMiddleBank.setDpStatus(DpStatus.NORMAL.getCode());
		dzLoanMiddleBank=dzLoanMiddleBankMapper.selectRefined(dzLoanMiddleBank);
		if(dzLoanMiddleBank!=null){
			dzLoanMiddleBank.initUpdateData(currentUser.getId(), currentUser.getName());
			dzLoanMiddleBank.setCardnoRefund(ywOpenCardHzicbc.getCardno());
			dzLoanMiddleBankMapper.updateByPrimaryKeySelective(dzLoanMiddleBank);
		}else{
			dzLoanMiddleBank=new DzLoanMiddleBank();
			dzLoanMiddleBank.setBusiId(busiId);
			dzLoanMiddleBank.setCusId(cusId);
			dzLoanMiddleBank.setCardnoRefund(ywOpenCardHzicbc.getCardno());
			dzLoanMiddleBank.initInsertData(currentUser.getId(), currentUser.getName());
			dzLoanMiddleBankMapper.insert(dzLoanMiddleBank);
		}
		
		DhLoanAfter dhLoanAfter=new DhLoanAfter();
		dhLoanAfter.setBusiId(busiId);
		dhLoanAfter.setDpStatus(DpStatus.NORMAL.getCode());
		dhLoanAfter=dhLoanAfterMapper.selectRefined(dhLoanAfter);
		if(dhLoanAfter!=null){
			dhLoanAfter.initUpdateData(currentUser.getId(), currentUser.getName());
			dhLoanAfter.setBankStatementDate(ywOpenCardHzicbc.getBankStatementDate());
			dhLoanAfter.setDateRepayment(ywOpenCardHzicbc.getRepaymentDate());
			dhLoanAfterMapper.updateByPrimaryKeySelective(dhLoanAfter);
		}else{
			dhLoanAfter=new DhLoanAfter();
			dhLoanAfter.setBusiId(busiId);
			dhLoanAfter.setCusId(cusId);
			dhLoanAfter.setBankStatementDate(ywOpenCardHzicbc.getBankStatementDate());
			dhLoanAfter.setDateRepayment(ywOpenCardHzicbc.getRepaymentDate());
			dhLoanAfter.initInsertData(currentUser.getId(), currentUser.getName());
			dhLoanAfterMapper.insert(dhLoanAfter);
		}
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(busiId);
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywBusiness.setOpenCardStatus(OpenCardStatus.KAI_KA_CHENG_GONG.getCode());
		ywBusiness.initUpdateData(currentUser.getId(), currentUser.getName());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(busiId);
		ywOpenCardHzicbc1.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.KAI_KA_CHENG_GONG.getCode());
		ywOpenCardHzicbc1.initUpdateData(currentUser.getId(), currentUser.getName());
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
	}

	@Override
	public void saveOpencardLine(YwOpenCardHzicbc ywOpenCardHzicbc,
			AbstractSysUser currentUser) {
		String busiId=ywOpenCardHzicbc.getBusiId();
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(busiId);
		ywOpenCardHzicbc1.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1!=null){
			ywOpenCardHzicbc1.initUpdateData(currentUser.getId(), currentUser.getName());
			ywOpenCardHzicbc1.setBankStatementDate(ywOpenCardHzicbc.getBankStatementDate());
			ywOpenCardHzicbc1.setRepaymentDate(ywOpenCardHzicbc.getRepaymentDate());
			ywOpenCardHzicbc1.setCardno(ywOpenCardHzicbc.getCardno());
			ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
		}else{
			ywOpenCardHzicbc1=new YwOpenCardHzicbc();
			ywOpenCardHzicbc1.setBusiId(busiId);
			ywOpenCardHzicbc1.setCusId(ywOpenCardHzicbc.getCusId());
			ywOpenCardHzicbc1.setBankStatementDate(ywOpenCardHzicbc.getBankStatementDate());
			ywOpenCardHzicbc1.setRepaymentDate(ywOpenCardHzicbc.getRepaymentDate());
			ywOpenCardHzicbc1.setCardno(ywOpenCardHzicbc.getCardno());
			ywOpenCardHzicbc1.initInsertData(currentUser.getId(), currentUser.getName());
			ywOpenCardHzicbcMapper.insert(ywOpenCardHzicbc1);
		}
		
	}

	@Override
	public void updateLineOpenCardStatus(YwOpenCardHzicbc ywOpenCardHzicbc,
			AbstractSysUser currentUser) {
		String reasonBack=ywOpenCardHzicbc.getReasonBack();
		
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbc.getBusiId());
		ywOpenCardHzicbc1.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywOpenCardHzicbc1.setReasonBack(reasonBack);
		ywOpenCardHzicbc1.setOpenCardStatus(OpenCardStatus.XIAN_XIA_CHU_LI.getCode());
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywBusiness.setOpenCardStatus(OpenCardStatus.XIAN_XIA_CHU_LI.getCode());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);	
		
		YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
		ywOpenCardHzicbcLog.initInsertData(currentUser.getId(), currentUser.getName());
		ywOpenCardHzicbcLog.setBusiId(ywBusiness.getId());
		ywOpenCardHzicbcLog.setCusId(ywBusiness.getCustomerId());
		ywOpenCardHzicbcLog.setLogType(1);
		ywOpenCardHzicbcLog.setOpencardStatus(OpenCardStatus.XIAN_XIA_CHU_LI.getCode().toString());
		ywOpenCardHzicbcLog.setRemarks(reasonBack);
		ywOpenCardHzicbcLogMapper.insert(ywOpenCardHzicbcLog);
	}

	@Override
	public void updateStopOpenCardStatus(YwOpenCardHzicbc ywOpenCardHzicbc,
			AbstractSysUser currentUser) {
		ywOpenCardHzicbc=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc);
		if(ywOpenCardHzicbc==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywOpenCardHzicbc.setOpenCardStatus(OpenCardStatus.ZHONG_ZHI_KAI_KA.getCode());
		ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc);
		
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(ywOpenCardHzicbc.getBusiId());
		if(ywBusiness==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		ywBusiness.setOpenCardStatus(OpenCardStatus.ZHONG_ZHI_KAI_KA.getCode());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);	
		
		YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
		ywOpenCardHzicbcLog.initInsertData(currentUser.getId(), currentUser.getName());
		ywOpenCardHzicbcLog.setBusiId(ywBusiness.getId());
		ywOpenCardHzicbcLog.setCusId(ywBusiness.getCustomerId());
		ywOpenCardHzicbcLog.setLogType(1);
		ywOpenCardHzicbcLog.setOpencardStatus(OpenCardStatus.ZHONG_ZHI_KAI_KA.getCode().toString());
		ywOpenCardHzicbcLogMapper.insert(ywOpenCardHzicbcLog);
	}

	@Override
	public void updateHzicbcOpencardStatus(YwOpenCardHzicbc ywOpenCardHzicbc
			) {
		// Integer opencardStatus=ywOpenCardHzicbc.getOpenCardStatus();
		String icbcOpencardStatus = ywOpenCardHzicbc.getHzicbcOpenCardStatus();
		YwOpenCardHzicbc ywOpenCardHzicbc1=new YwOpenCardHzicbc();
		ywOpenCardHzicbc1.setBusiId(ywOpenCardHzicbc.getBusiId());
		ywOpenCardHzicbc1.setDpStatus(DpStatus.NORMAL.getCode());
		ywOpenCardHzicbc1=ywOpenCardHzicbcMapper.selectRefined(ywOpenCardHzicbc1);
		if(ywOpenCardHzicbc1==null){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		if(!icbcOpencardStatus.equals(ywOpenCardHzicbc1.getHzicbcOpenCardStatus())){
			ywOpenCardHzicbc1.setHzicbcOpenCardStatus(ywOpenCardHzicbc.getHzicbcOpenCardStatus());
			ywOpenCardHzicbc1.setHzicbcOpenCardStatusTime(ywOpenCardHzicbc.getHzicbcOpenCardStatusTime());
			ywOpenCardHzicbc1.setHzicbcOpenCardStatusNote(ywOpenCardHzicbc.getHzicbcOpenCardStatusNote());
			ywOpenCardHzicbcMapper.updateByPrimaryKeySelective(ywOpenCardHzicbc1);
			
			YwOpenCardHzicbcLog ywOpenCardHzicbcLog=new YwOpenCardHzicbcLog();
//			ywOpenCardHzicbcLog.initInsertData(currentUser.getId(), currentUser.getName());
			ywOpenCardHzicbcLog.setBusiId(ywOpenCardHzicbc1.getId());
			ywOpenCardHzicbcLog.setLogType(2);
			ywOpenCardHzicbcLog.setOpencardStatus(ywOpenCardHzicbc.getHzicbcOpenCardStatus());
			ywOpenCardHzicbcLog.setRemarks(ywOpenCardHzicbc.getHzicbcOpenCardStatusNote());
//			sf.parse(ywOpenCardHzicbc.getHzicbcOpenCardStatusTime()).currentTimeMillis();
//			long time=ZhppDateUtil.strToTimestamp(ywOpenCardHzicbc.getHzicbcOpenCardStatusTime(), "yyyy-MM-dd HH:mm:ss").getTime();
//			Integer current = (int)(time / 1000);
			ywOpenCardHzicbcLog.initInsertData("00000000000000000000000000000000","开卡状态更新");
//			ywOpenCardHzicbcLog.setId(UUIDFactory.getUUID());
//	        ywOpenCardHzicbcLog.setCreated(current);
//	        ywOpenCardHzicbcLog.setModified(current);
	        ywOpenCardHzicbcLog.setDpStatus(DpStatus.NORMAL.getCode());
	        ywOpenCardHzicbcLog.setTypeData(TypeData.SYSTEM.getCode());
			ywOpenCardHzicbcLogService.saveUpdateAndDelete(ywOpenCardHzicbcLog, OperateType.add);
		}
	}
}
